package net.lab1024.sa.admin.listener;

import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;

import javax.annotation.Resource;


/**
 * 子流程并行网关结束监听器
 */
public class SubProcessStartListener implements ExecutionListener {

    @Resource
    private TaskService taskService;

    private HistoryService historyService;

    private RuntimeService runtimeService;


    @Override
    public void notify(DelegateExecution execution) {
        //设置发起人为前一个节点的执行人
        TaskQuery taskQuery = taskService.createTaskQuery().executionId(execution.getId());
        Task task = taskQuery.singleResult();
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
            .processInstanceId(execution.getProcessInstanceId())
            .singleResult();
        HistoricActivityInstance historicActivityInstance = historyService.createHistoricActivityInstanceQuery()
            .processInstanceId(execution.getProcessInstanceId())
            .activityId(historicProcessInstance.getStartActivityId()).
            finished().singleResult();

        String assignee = historicActivityInstance.getAssignee();
        taskService.setAssignee(task.getId(), assignee);


    }
}

